<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: yangweijie <yangweijiester@gmail.com> <code-tech.diandian.com>
// +----------------------------------------------------------------------

namespace Help\Model;

use Think\Model;

/**
 * Class HelpSnapshotModel  帮忙快照模型
 * @package Help\Model
 * @author:xjw129xjt xjt@ourstu.com
 */
class HelpSnapshotModel extends Model
{

    protected $tableName = 'help_snapshot';


    public function addSnapshot($help_id = 0, $stage = 1, $uid = 0)
    {
        $uid = $uid ? $uid : is_login();
        $time = time();
        $data = array();
        if ($stage == 1) {
            $help = D('Help')->getHelp($help_id);
            $data_help = array('uid' => $uid, 'flag' => 'help', 'time' => $time, 'data' => serialize(array('help'=>$help)), 'help_id' => $help_id, 'stage' => 1 );
            array_push($data, $data_help);
        }
        if($stage == 2){
            $help = D('Help')->getHelp($help_id);
            $work = D('HelpWorks')->getWorks($help_id);

           $attend = D('HelpAttend')->getAcceptByHelpId($help_id);
            foreach($attend as $k => $v){


            $check = check_is_evaluate($help_id,$v['uid']) && check_publisher_is_evaluate($help_id ,$help['uid'] ,$v['uid']);
            if($check){
                $evaluate = D('HelpEvaluate')->getEvaluate(array('help_id'=>$help_id,'uid'=>$v['uid'],'to_uid'=>$help['uid']));
                $to_evaluate = D('HelpEvaluate')->getEvaluate(array('help_id'=>$help_id,'uid'=>$help['uid'],'to_uid'=>$v['uid']));

                $evaluate[0]['user'] = query_user(array('uid', 'username', 'nickname', 'space_url', 'space_link', 'avatar64', 'avatar128', 'rank_html', 'signature','address','doing','fans'), $evaluate[0]['uid']);
                $evaluate[0]['to_user'] = query_user(array('uid', 'username', 'nickname', 'space_url', 'space_link', 'avatar64', 'avatar128', 'rank_html', 'signature','address','doing','fans'), $evaluate[0]['to_uid']);
                $evaluate[0]['attaches'] = explode(',',$evaluate[0]['attach_ids']);
                $to_evaluate[0]['user'] = query_user(array('uid', 'username', 'nickname', 'space_url', 'space_link', 'avatar64', 'avatar128', 'rank_html', 'signature','address','doing','fans'), $to_evaluate[0]['uid']);
                $to_evaluate[0]['to_user'] = query_user(array('uid', 'username', 'nickname', 'space_url', 'space_link', 'avatar64', 'avatar128', 'rank_html', 'signature','address','doing','fans'), $to_evaluate[0]['to_uid']);
                $to_evaluate[0]['attaches'] = explode(',',$to_evaluate[0]['attach_ids']);


                $data_evaluate =  $data_one = array('uid' => $v['uid'], 'flag' => 'evaluate', 'time' => $time,  'data' => serialize(array('evaluate'=>$evaluate[0])), 'help_id' => $help_id, 'stage' => $work?3:2);
                array_push($data, $data_evaluate);

                $data_evaluate_to =   array('uid' => $v['uid'], 'flag' => 'to_evaluate', 'time' => $time,  'data' => serialize(array('to_evaluate'=>$to_evaluate[0])), 'help_id' => $help_id, 'stage' => $work?3:2);
                array_push($data, $data_evaluate_to);
            }
            if($work){
                $data_work = array('uid' => $v['uid'], 'flag' => 'work', 'time' => $time, 'data' => serialize(array('work'=>$work)), 'help_id' => $help_id, 'stage' => $check?3:2);
                array_push($data, $data_work);
            }
            $data_help = array('uid' => $v['uid'], 'flag' => 'help', 'time' => $time, 'data' => serialize(array('help'=>$help)), 'help_id' => $help_id, 'stage' => $check && $work?3:2 );
            array_push($data, $data_help);

            }
            unset($k,$v);
        }
        $res = $this->addAll($data);
        return $res;
    }

    public function delSnapshot($help_id = 0, $stage = 1, $uid = 0,$flag=''){
        $res = $this->where(array('help_id'=>$help_id,'stage'=>$stage,'uid'=>$uid,'flag'=>$flag))->delete();
        return $res;
    }

    public function getSnapshot($help_id = 0,$uid=0){
        $uid = $uid ? $uid : is_login();
        $snapshot = $this->where(array('help_id'=>$help_id,'uid'=>$uid))->order('stage desc')->select();
        dump($snapshot);
        $stage = $snapshot[0]['stage'];
        foreach($snapshot as $k=>&$v){
            if($v['stage'] != $stage){
                unset($snapshot[$k]);
            }
            $v['data_array'] = unserialize($v['data']);
            $return[$v['flag']] = $v['data_array'][$v['flag']];
        }
        unset($k,$v);
        $return['stage'] = $stage;
        return $return;

    }


}